package com.lagou.controller;

import com.lagou.domain.Employee;
import com.lagou.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;


    /**
     * 新增员工
     * @param employee
     * @return
     */
    @RequestMapping("/saveEmployee")
    public String saveEmployee(Employee employee) {
        int i = employeeService.saveEmployee(employee);

        System.out.println("saveEmployee------>"+i);
        // 跳转到findAll方法从新查询一次数据库进行数据的遍历展示
        return "redirect:/emp/queryAllEmp";

    }

    /**
     * 获取员工详情
     * @param empId
     * @return
     */
    @RequestMapping("/queryEmpByEmpId")
    public String queryEmpByEmpId(Model model, Integer empId) {

        System.out.println("empId------>"+empId);

        Employee employee = employeeService.queryEmpByEmpId(empId);
        model.addAttribute("employee",employee);

        System.out.println("queryEmpByEmpId------>"+employee);

        return "update";
    }


    /**
     * 查询所有员工
     * @return
     */
    @RequestMapping("/queryAllEmp")
    public String queryAllEmp(Model model) {
        List<Employee> employeeList = employeeService.queryAllEmp();
        model.addAttribute("employeeList",employeeList);

        for (Employee employee : employeeList) {
            System.out.println("employeeList------>"+employee);
        }

        return "employeeList";
    }

    /**
     * 更新员工信息
     * @param employee
     * @return
     */

    @RequestMapping("/updateEmployee")
    public String updateEmployee(Employee employee) {
        int i = employeeService.updateEmployee(employee);
        System.out.println("updateEmployee------>"+i);
        return "redirect:/emp/queryAllEmp";
    }


    /**
     * 删除员工信息
     * @param ids
     * @return
     */

    @RequestMapping("/deleteBatch")
    public String deleteBatch(Integer[] ids) {
        employeeService.deleteBatch(ids);
        return "redirect:/emp/queryAllEmp";
    }


    /**
     * 删除员工信息
     * @param empId
     * @return
     */

    @RequestMapping("/deleteEmp")
    public String deleteEmp(Integer empId) {
        employeeService.deleteEmp(empId);
        return "redirect:/emp/queryAllEmp";
    }


}
